package com.ebai.product.management.repository.system;

import java.util.List;

import javax.persistence.Table;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.ebai.product.management.model.entity.User;
import com.ebai.product.management.model.vojo.RoleUserVo;

@Repository
@Table(name="user")
@Qualifier("userRepository")
public interface UserRepository extends CrudRepository<User, String>, JpaSpecificationExecutor<User> {

    public User save(User user);

    public void deleteByPid(String pid);

    public void delete(User entity);

    public User findByPid(String pid);

    public List<User> findAll();

    Page<User> findAll(Specification<User> specification, Pageable pageable);
    
    @Query(value = "select new com.ebai.product.management.model.vojo.RoleUserVo(t1.pid,t1.loginName,t1.userName,t1.mobileNo,t1.isEnable,t2.rolePid) "
    		+ "from User t1 left join RoleUser t2 on t1.pid = t2.userPid and t2.rolePid =:rolePid ",
            countQuery = "select count(*) from User t1 left join RoleUser t2 on t1.pid = t2.userPid and t2.rolePid =:rolePid")
    Page<RoleUserVo> findAllByRole(@Param("rolePid") String rolePid, Pageable pageable);

    @Query("select t from User t where t.loginName=:loginName")
    public User findUserByLoginName(@Param("loginName") String loginName);

    @Query("select t from User t where t.loginName=:loginName and t.loginPasswd=:loginPasswd")
    public User findUserByLoginName(@Param("loginName") String loginName, @Param("loginPasswd") String loginPasswd);

    @Query("select t from User t where t.loginName=:loginName")
    public List<User> queryUserListByLoginName(@Param("loginName") String loginName);
}
